home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / movessp / part01 / movessp.asm
Assembly Source File  |  1990-03-26  |  7KB  |  289 lines

  1. ; MoveSSP.a
  2. ; Copy the system's stack from Chip RAM to Fast RAM, generally to be called
  3. ; as part of one's Startup-Sequence.
  4. ;
  5. ; Written by Christoper A. Wichura (caw@miroc.chi.il.us)
  6. ; Created: 3/4/90
  7. ;
  8. ; This is really a conversion of MoveSSP.c by Roger Uzun into 68k code,
  9. ; with various `optimizations' being made as well.
  10. ;
  11. ; The arp library is required to use this program.  I used it for its
  12. ; FPrintf function as well as getting an easy way out of not having to
  13. ; decide if I needed to close the stdout filehanle, which is opened with
  14. ; ArpOpen() and closed (if needed) by ArpExit().
  15. ;
  16. ; It is fully residentiable, though to do so would be stupid as it will
  17. ; not do anything after the first invocation.
  18. ;
  19. ; This file supports being called by WorkBench.  However, you will have
  20. ; to supply your own `.info' file for it (if anyone comes up with a really
  21. ; nice looking one then UUencode it and send it to me).
  22. ;
  23. ; I moved it to ML to make it smaller (i.e., no longer needed the Lattice
  24. ; startup code which eats a bunch of memory up).  I also added some
  25. ; messages to let you know what has happened, which MoveSSP.c didn't do.
  26. ; I also made it use CopyMem() to copy the old SSP over into the new one.
  27. ; Q:  Is the system stack quarenteed to meet the requirements for a
  28. ; CopyMemQuick() call?  Also, does CopyMem() automatically recognize if the
  29. ; parameters passed to it are valid to CopyMemQuick() and forward the call
  30. ; instead of trying to handle it itself?  Mail me...
  31. ;
  32. ; I have not noticed any tremendous speed increase in using this program,
  33. ; but it can't really hurt.  The people who will get the biggest benefit
  34. ; from it are those with 32 bit RAM.
  35. ;
  36. ; My AutoDocs say that the UserState() function is broken.  Welp, it is
  37. ; used in this file.  It doesn't seem to cause any problems, but just what
  38. ; about it is broken?  Somebody send me mail...
  39.  
  40.  NOLIST
  41.  INCLUDE "libraries/ArpBase.i"
  42.  INCLUDE "exec/execbase.i"
  43.  INCLUDE "exec/memory.i"
  44.  INCLUDE "libraries/dosextens.i"
  45.  LIST
  46.  
  47.  XREF _LVOOpenLibrary
  48.  XREF _LVOCloseLibrary
  49.  XREF _LVOForbid
  50.  XREF _LVOWaitPort
  51.  XREF _LVOGetMsg
  52.  XREF _LVOReplyMsg
  53.  XREF _LVOAllocMem
  54.  XREF _LVOCopyMem
  55.  XREF _LVODisable
  56.  XREF _LVOEnable
  57.  XREF _LVOSuperState
  58.  XREF _LVOUserState
  59.  
  60. CALL    MACRO
  61.     jsr _LVO\1(a6)
  62.     ENDM
  63.  
  64.   SECTION MoveSSP,CODE
  65.  
  66. ; arp resident tag goes here
  67.     RESIDENT 4*1024
  68.  
  69. init:
  70.     movem.l    d2-d7/a2-a5,-(sp)
  71.     movea.l    4,a6
  72.     lea    ARPlib(pc),a1
  73.     moveq    #39,d0
  74.     CALL    OpenLibrary
  75.     tst.l    d0
  76.     bne.s    __main
  77.  
  78.     lea    DOSlib(pc),a1
  79.     CALL    OpenLibrary
  80.     tst.l    d0
  81.     beq.s    bomb
  82.  
  83.     move.l    d0,a6
  84.     CALL    Output
  85.     move.l    d0,d1
  86.     beq.s    bomb
  87.  
  88.     lea    Msg1(pc),a0
  89.     move.l    a0,d2
  90.     moveq    #Msg1Len,d3
  91.     CALL    Write
  92.  
  93.     move.l    a6,a1
  94.     movea.l    4,a6
  95.     CALL    CloseLibrary
  96.     moveq    #0,d0
  97.  
  98. bomb:
  99.     addq.l    #8,sp
  100.     rts
  101.  
  102. WBMSG    EQUR    d7
  103. LOMEM    EQUR    d6
  104. HIMEM    EQUR    d5
  105. SIZE    EQUR    d4
  106. STDOUT    EQUR    d3
  107. NEWMEM    EQUR    a2
  108. MYPROC    EQUR    a3
  109.  
  110. __main:
  111.     movea.l    a6,a5        ; save exec base pointer
  112.     movea.l    d0,a6        ; put arp base where we can use it
  113.  
  114.     CALL    Output        ; get stdout handle
  115.     move.l    d0,STDOUT
  116.  
  117.     move.l    ThisTask(a5),MYPROC    ; get pointer to our process
  118.  
  119. ; check for workbench
  120.     moveq    #0,WBMSG    ; clear wb flag
  121.  
  122.     tst.l    pr_CLI(MYPROC)
  123.     bne.s    FromCLI
  124.  
  125.     exg    a5,a6        ; get exec base
  126.     lea    pr_MsgPort(MYPROC),a0
  127.     CALL    WaitPort
  128.     lea    pr_MsgPort(MYPROC),a0
  129.     CALL    GetMsg
  130.     move.l    d0,WBMSG    ; save wbench startup message
  131.     exg    a5,a6        ; get back arp base
  132.  
  133. ; we need a console window for workbench users
  134.  
  135.     lea    Console.MSG(pc),a0
  136.     move.l    a0,d1
  137.     move.l    #MODE_NEWFILE,d2
  138.  
  139. ; we use arpopen to get the console.  thus the ArpExit call at the end of
  140. ; this file will free it for us automatically if we got it.
  141.  
  142.     CALL    ArpOpen
  143.  
  144.     move.l    d0,STDOUT    ; did we get the console
  145.     beq    Exit    ; no so die
  146.  
  147.     lsl.l    #2,d0
  148.     movea.l    d0,a0
  149.  
  150.     move.l    fh_Type(a0),pr_ConsoleTask(a3)
  151.  
  152. FromCLI:
  153.     lea    Banner.MSG(pc),a0    ; print a blurb
  154.     move.l    STDOUT,d0    ; get stdout
  155.     CALL    FPrintf
  156.  
  157.     move.l    SysStkLower(a5),LOMEM    ; get low stack ptr
  158.     move.l    SysStkUpper(a5),HIMEM    ; get high stack ptr
  159.  
  160. ; check if we are already in fast memory
  161.     cmpi.l    #$200000,HIMEM
  162.     blt.s    1$
  163.     cmpi.l    #$C00000,HIMEM
  164.     bcc.s    1$
  165.  
  166.     lea    AlreadyFast.MSG(pc),a0
  167.     move.l    STDOUT,d0    ; get stdout
  168.     CALL    FPrintf
  169.     bra    Exit
  170.  
  171. ; we need to move the thing over so figure out its size.  we will print
  172. ; its current start address before moving it, though.
  173.  
  174. 1$:    move.l    HIMEM,SIZE
  175.     sub.l    LOMEM,SIZE
  176.  
  177.     move.l    SIZE,-(sp)    ; push size  onto stack for printf
  178.     move.l    HIMEM,-(sp)    ; push himem onto stack for printf
  179.     move.l    LOMEM,-(sp)    ; push lomem onto stack for printf
  180.  
  181.     movea.l    sp,a1        ; get address of args array
  182.     lea    OldLoc.MSG(pc),a0    ; get message to print
  183.     move.l    STDOUT,d0    ; get stdout
  184.     CALL    FPrintf        ; call ARP print command
  185.     lea    12(sp),sp    ; pop values off stack
  186.  
  187. ; we need to allocate a new stack in fast memory for these folks
  188.  
  189.     move.l    SIZE,d0
  190.     move.l    #MEMF_FAST|MEMF_CLEAR,d1
  191.     exg    a5,a6
  192.     CALL    AllocMem
  193.     exg    a5,a6
  194.  
  195.     tst.l    d0
  196.     bne.s    2$
  197.  
  198.     lea    NoFast.MSG(pc),a0
  199.     move.l    STDOUT,d0    ; get stdout
  200.     CALL    FPrintf
  201.     bra    Exit
  202.  
  203. 2$    movea.l    d0,NEWMEM
  204.  
  205. ; ok we have new memory so now we need to copy the stack over
  206.  
  207.     exg    a5,a6    ; get execbase in a6
  208.     CALL    Disable    ; freeze the system while we do the copy
  209.  
  210.     movea.l    LOMEM,a0
  211.     movea.l    NEWMEM,a1
  212.     move.l    SIZE,d0
  213.     CALL    CopyMem
  214.  
  215. ; now fix pointers in exec base
  216.  
  217.     move.l    NEWMEM,HIMEM    ; make new himem pointer
  218.     add.l    SIZE,HIMEM
  219.  
  220.     move.l    NEWMEM,SysStkLower(a6)
  221.     move.l    HIMEM,SysStkUpper(a6)
  222.  
  223.     CALL    SuperState    ; go to supervisor mode
  224.     sub.l    LOMEM,d0    ; find position in old stack
  225.     add.l    NEWMEM,d0    ; get position in new stack
  226.     CALL    UserState    ; return to user state
  227.     
  228.     CALL    Enable        ; re-enable interrupts
  229.     exg    a5,a6        ; reload ARP base
  230.  
  231. ; ok we have fixed everything up so print new address of stack
  232.  
  233.     move.l    NEWMEM,-(sp)    ; push new location onto stack for printf
  234.     movea.l    sp,a1        ; get address of args array
  235.     lea    NewLoc.MSG(pc),a0    ; get message to print
  236.     move.l    STDOUT,d0    ; get stdout
  237.     CALL    FPrintf        ; call ARP print command
  238.     addq    #4,sp        ; pop value off stack
  239.  
  240. Exit:
  241.     tst.l    WBMSG        ; started from workbench?
  242.     beq.s    1$
  243.  
  244. ; if we started from workbench then call dos' Delay() function to give
  245. ; them time to see what we said
  246.  
  247.     move.l    #300,d1
  248.     CALL    Delay
  249.  
  250.     exg    a5,a6        ; get exec base
  251.     CALL    Forbid        ; forbid and return startup message to
  252.     movea.l    WBMSG,a1    ; workbench
  253.     CALL    ReplyMsg
  254.     exg    a5,a6        ; get back arp base
  255.  
  256. 1$    movem.l    (sp)+,d2-d7/a2-a5
  257.     moveq    #0,d0
  258.     CALL    ArpExit
  259.  
  260. ; here we have all the text and whatnot for this program
  261.  
  262. DOSlib    dc.b "dos.library",0
  263.  
  264. Msg1    dc.b "You need "
  265. ARPlib    dc.b "arp.library",0
  266.     dc.b " V39+",10,0
  267. Msg1Len    EQU *-Msg1
  268.  
  269. Console.MSG:
  270.     dc.b    'CON:40/30/560/45/MoveSSP',0
  271.  
  272. Banner.MSG:
  273.     dc.b    'MoveSSP v1.0 ',$a9,' 1990 by Christoper A. Wichura '
  274.     dc.b    '(caw@miroc.chi.il.us)',10,0
  275.  
  276. AlreadyFast.MSG:
  277.     dc.b    'SSP is already located in fast memory.',10,0
  278.  
  279. NoFast.MSG:
  280.     dc.b    'Could not allocate fast memory for new SSP.',10,0
  281.  
  282. OldLoc.MSG:
  283.     dc.b    'SSP currently located at $%08lx-$%08lx (%ld bytes).',10,0
  284.  
  285. NewLoc.MSG:
  286.     dc.b    'Moved to $%08lx in fast memory.',10,0
  287.  
  288.     END
  289.